var $ = getApp(), methods = require("../methods.js");
var interval = null;
Page(Object.assign({}, methods, {
	data: {
		tabnum: 1,
		buydialogHidden: true,
		num: 1,
		isfavorite: false,
		btntype: 1,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		nodata: 0,
		userinfo:[],
		djsday:'00',
		djshour:'00',
		djsmin:'00',
		djssec:'00',
	},
	_onLoad: function () {
		var that = this
		var id = 0;
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.get('kanjia/product', { id: id }, function (res) {
			if (res.status == 0) {
				$.alert(res.msg); return;
			}
			var product = res.product;
			var sysset = res.sysset;

			var pagecontent = JSON.parse(product.detail);
			var _bannerindex = [];
			for(var i in pagecontent){
				_bannerindex[i] = 0;
			}
			sysset.textset = that.data.textset
			that.setData({
				product: product,
				sysset:sysset,
				shopset:res.shopset,
				pagecontent:pagecontent,
				_bannerindex:_bannerindex,
				joinlist:res.joinlist,
				nowtime:res.nowtime,
				imJoin:res.imJoin,
				title:product.name,
				isfavorite:res.isfavorite,
				sharepic:product.pics[0]
			});
			clearInterval(interval);
			interval = setInterval(function(){
				that.setData({nowtime: that.data.nowtime+1})
				that.getdjs();
			},1000)

			wx.setNavigationBarTitle({
				title: product.name
			});
		})
	},
	getdjs:function(){
		var that = this
		if(that.data.product.starttime*1 > that.data.nowtime*1){
			var totalsec = that.data.product.starttime*1 - that.data.nowtime*1; 
		}else{
			var totalsec = that.data.product.endtime*1 - that.data.nowtime*1; 
		}
		if(totalsec <=0){
			 that.setData({djsday:'00',djshour:'00',djsmin:'00',djssec:'00'})
		}else{
			var date = Math.floor(totalsec/86400);
			var houer = Math.floor((totalsec - date*86400)/3600);
			var min = Math.floor((totalsec - date*86400 - houer *3600)/60);
			var sec = totalsec - date*86400 - houer*3600 - min*60
			var djsday = (date<10?'0':'')+date
			var djshour = (houer<10?'0':'')+houer
			var djsmin = (min<10?'0':'')+min
			var djssec = (sec<10?'0':'')+sec
			that.setData({djsday:djsday,djshour:djshour,djsmin:djsmin,djssec:djssec})
		}
	},
	joinin: function (e) {
		var type = e.currentTarget.dataset.type
		var that = this;
		var proid = that.data.product.id;
		$.goto('/pages/kanjia/join?proid=' + proid);
	},
	//收藏操作
	addfavorite: function () {
		var that = this;
		var proid = that.data.product.id;
		$.post('kanjia/addfavorite', { proid: proid,type:'kanjia'}, function (data) {
			if (data.status == 1) {
				that.setData({ isfavorite: !that.data.isfavorite });
			}
			$.success(data.msg);
		}, '加载中');

	},
	tabClick: function (e) {
		this.setData({
			tabnum: e.currentTarget.dataset.num
		})
	},
	shareClick:function(){
		this.setData({sharetypevisible:true})
	},
	handleClickMask:function(){
		this.setData({sharetypevisible:false})
	},
	bindGetUserInfo: function (e) {
		var that = this
		var userInfo = e.detail.userInfo
		if (e.detail.userInfo) {
			$.post('pub/userinfo', { userInfo: userInfo,encryptedData:e.detail.encryptedData,iv: e.detail.iv}, function (data) {
				that.showPoster();
			});
		}
	},
	onUnload:function(){
		clearInterval(interval);
	},
	showPoster:function(){
		var that = this;
		that.setData({showposter:true,sharetypevisible:false});
		$.post('kanjia/getposter',{proid:that.data.product.id},function(data){
			if(data.status==0){
				$.alert(data.msg);
			}else{
				that.setData({posterpic:data.poster})
			}
		},'努力生成中')
	},
	posterDialogClose:function(){
		this.setData({showposter:false});
	}
}))